﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Text.RegularExpressions;

namespace TVGuide.Common
{
    public static class Utils
    {

        /// <summary>
        /// Sending e-mail message to arm.tvguide@gmail.com adress, with the parsing issue described.
        /// </summary>
        /// <param name="message">Body of the e-mail.</param>
        public static void SendNotificationMail(string message)
        {

            var client = new SmtpClient("smtp.gmail.com", 587)
            {
                Credentials = new NetworkCredential("arm.tvguide@gmail.com", "tvguidearm"),
                EnableSsl = true
            };
            //client.Send("arm.tvguide@gmail.com", "arm.tvguide@gmail.com", "Failed action", message);
        }

        /// <summary>
        /// Chopping all HTML tags from the string.
        /// </summary>
        /// <param name="input">Input string</param>
        /// <returns></returns>
        public static string removeHTMLTags(string input)
        {
            Regex rgxTagCutter = new Regex("</?[^>]*>");
            string stringWithoutTags = rgxTagCutter.Replace(input, " ");
            return stringWithoutTags;
        }

        /// <summary>
        /// Extension method for MatchCollection.
        /// </summary>
        /// <param name="inputCollection">Input MatchCollection.</param>
        /// <returns>
        /// Returns HTML tag free string list from MatchCollection.
        /// </returns>
        public static List<string> removeHTMLTags(this MatchCollection inputCollection)
        {
            List<string> outputList = new List<string>();
            foreach (Match inputMatch in inputCollection)
            {

                outputList.Add(removeHTMLTags(inputMatch.Value));
            }


            return outputList;
        }

        /// <summary>
        /// Seperates date and program name from string represented as: 00:00 Some TV Program
        /// </summary>
        /// <param name="input">Input string to extract time and program name.</param>
        /// <param name="time">Time of the program.</param>
        /// <param name="programName">Program name.</param>
        public static void seperateTimeProgramName(string input, out int hour, out int minutes, out string programName)
        {         
            string programDateStringPattern = @"(\d{2}):(\d{2})";
            Regex rgxProgramDateString = new Regex(programDateStringPattern);
            Match dateMatch = rgxProgramDateString.Match(removeHTMLTags(input));
            hour = Int32.Parse(dateMatch.Groups[1].Value);
            minutes = Int32.Parse(dateMatch.Groups[2].Value);
            
            string programNameStringPattern = @"\d{2}:\d{2}(.*)";
            Regex rgxProgramNameString = new Regex(programNameStringPattern);
            Match programNameMatch = rgxProgramNameString.Match(input);
            programName = programNameMatch.Groups[1].Value;
        }

        public static void seperateTimeProgramName(string input, out int hour, out int minutes)
        {
            string dummy;
            seperateTimeProgramName(input, out hour, out minutes, out dummy);
        }


        public static string EscapeSpacialSymbols(string input)
        {
            string ntv = "NTV+";
            Regex rgxSpacialSymbol = new Regex("([+*?])");
            Match match = rgxSpacialSymbol.Match(input);
            string stringWithoutTags = rgxSpacialSymbol.Replace(input, "\\" + rgxSpacialSymbol.Match(ntv).Groups[1].Value);
            return stringWithoutTags;
        }
       
    }
}
